﻿using System.Linq;
using Basmus.Conel;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace Basmus.Tests
{
    [TestClass()]
    public class ValueTypeEntityBindingTest
    {


        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        public void ValueTypeEntityBindingConstructorTestHelper<TElement>()
            where TElement : struct
        {
            // Arrange & Act
            var target = new ValueTypeEntityBinding<TElement>();

            //Assert
            Assert.IsTrue(target.ElementType == typeof(TElement));
        }

        [TestMethod()]
        public void ValueTypeEntityBindingConstructorTest()
        {
            var elementTypes = TypeHelper.ValueTypes();
            foreach (var elementType in elementTypes)
            {
                typeof (ValueTypeEntityBindingTest)
                    .GetMethod("ValueTypeEntityBindingConstructorTestHelper")
                    .MakeGenericMethod(elementType)
                    .Invoke(this, null);
            }
        }

        public void GetElementsTestHelper<TElement>()
            where TElement : struct
        {
            var entitycollection = ContainerHelper.GetContainersOfEntity<TElement>();
            foreach (var entity in entitycollection)
            {
                //Arrange
                var target = new ValueTypeEntityBinding<TElement>();
                var expectedElements = Enumerable.Empty<TElement>().DefaultIfEmpty(entity);

                //Act
                var actualElements = target.GetElements(entity).Cast<TElement>();

                //Assert
                Assert.IsTrue(expectedElements.Count() == actualElements.Count());
                for (int i = 0; i < expectedElements.Count(); i++)
                {
                    Assert.AreEqual(expectedElements.ElementAt(i), actualElements.ElementAt(i));
                }
            }
        }

        [TestMethod()]
        public void GetElementsTest()
        {
            var elementTypes = TypeHelper.ValueTypes();
            foreach (var elementType in elementTypes)
            {
                typeof(ValueTypeEntityBindingTest)
                    .GetMethod("GetElementsTestHelper")
                    .MakeGenericMethod(elementType)
                    .Invoke(this, null);
            }
        }


        public void GetContainerTestHelper<TElement>()
            where TElement : struct
        {
            //Arrange
            var target = new ValueTypeEntityBinding<TElement>();
            var elementsCollection = ElementsHelper.GetElementsOf<TElement>();
            foreach (var elements in elementsCollection)
            {
                var arg = elements == null ? null : elements.Cast<object>().ToArray();
                var expectedConatiner = elements == null || !elements.Any() ? default(TElement) : elements.ElementAt(0);

                //Act
                var actualContainer = (TElement) target.GetContainer(arg);

                //Assert
                Assert.AreEqual(expectedConatiner, actualContainer);
            }
        }

        [TestMethod()]
        public void GetContainerTest()
        {
            var elementTypes = TypeHelper.ValueTypes();
            foreach (var elementType in elementTypes)
            {
                typeof(ValueTypeEntityBindingTest)
                    .GetMethod("GetContainerTestHelper")
                    .MakeGenericMethod(elementType)
                    .Invoke(this, null);
            }
        }

    }
}
